<?php

require_once 'vendor/autoload.php';

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

// 初始化Laravel应用
$app = require_once 'bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

// 测试前后端API一致性
echo "🧪 测试前后端API接口一致性\n";
echo "=====================================\n\n";

echo "1. 测试后端路由配置...\n";

// 获取所有路由
$routes = Route::getRoutes();
$menuRoutes = [];

foreach ($routes as $route) {
    if (strpos($route->uri(), 'menus') !== false) {
        $menuRoutes[] = [
            'uri' => $route->uri(),
            'methods' => $route->methods(),
            'action' => $route->getActionName()
        ];
    }
}

echo "📋 菜单相关路由:\n";
foreach ($menuRoutes as $route) {
    echo "  - " . implode('|', $route['methods']) . " " . $route['uri'] . " -> " . $route['action'] . "\n";
}

echo "\n2. 检查关键路由是否存在...\n";

// 检查 /api/menus/tree 路由
$treeRouteExists = false;
$userTreeRouteExists = false;

foreach ($menuRoutes as $route) {
    if ($route['uri'] === 'api/menus/tree') {
        $treeRouteExists = true;
        echo "  ✅ /api/menus/tree 路由存在\n";
    }
    if ($route['uri'] === 'api/menus/user-tree') {
        $userTreeRouteExists = true;
        echo "  ❌ /api/menus/user-tree 路由仍然存在（应该已被移除）\n";
    }
}

if (!$treeRouteExists) {
    echo "  ❌ /api/menus/tree 路由不存在\n";
}

if (!$userTreeRouteExists) {
    echo "  ✅ /api/menus/user-tree 路由已被移除\n";
}

echo "\n3. 检查前端调用...\n";

// 检查前端文件
$frontendFiles = [
    'frontend/src/api/menu.ts',
    'frontend/src/api/auth.ts',
    'frontend/test_system.html'
];

foreach ($frontendFiles as $file) {
    $content = file_get_contents("../$file");
    $hasUserTree = strpos($content, 'user-tree') !== false;
    $hasTree = strpos($content, '/menus/tree') !== false;

    $status = $hasTree && !$hasUserTree ? "✅" : "❌";
    echo "  $status $file: " . ($hasTree ? "使用 /menus/tree" : "未使用 /menus/tree") . ", " . (!$hasUserTree ? "未使用 user-tree" : "仍使用 user-tree") . "\n";
}

echo "\n4. 测试结果总结...\n";

$allGood = $treeRouteExists && !$userTreeRouteExists;

if ($allGood) {
    echo "✅ 所有检查通过！前后端接口已统一\n";
    echo "   前端调用: /api/menus/tree\n";
    echo "   后端提供: /api/menus/tree\n";
} else {
    echo "❌ 仍有问题需要修复\n";
    if (!$treeRouteExists) {
        echo "   - 后端 /api/menus/tree 路由不存在\n";
    }
    if ($userTreeRouteExists) {
        echo "   - 后端仍提供 /api/menus/user-tree 路由\n";
    }
}

echo "\n🎯 测试完成!\n";
